package com.app.hanoitips.travel.custlistview;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.hanoitips.travel.R;
//import com.app.hanoitips.travel.R.id;
//import com.app.hanoitips.travel.R.layout;
 
public class CustomeAdapter extends BaseAdapter implements Filterable{
    private Activity activity;
    private ArrayList<HashMap<String, String>> dataOrigion;
    private ArrayList<HashMap<String, String>> data;
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
 
    public CustomeAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
    	super();
        activity = a;
        dataOrigion=d;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }
 @Override
    public int getCount() {
        return data.size();
    }
 @Override
    public Object getItem(int position) {
        return position;
    }
 @Override
    public long getItemId(int position) {
        return position;
    }
 @Override 
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_row, null);
        
        TextView url = (TextView)vi.findViewById(R.id.tvUrl); // title
        TextView shopname = (TextView)vi.findViewById(R.id.tvShopName); // artist name
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image
        
        
        HashMap<String, String> shop = new HashMap<String, String>();
        shop = data.get(position);
        
        int imgID=activity.getResources().getIdentifier(shop.get(CustomizedListView.KEY_THUMB_URL), "raw", activity.getPackageName());
                        // Setting all values in listview
        url.setText(shop.get(CustomizedListView.KEY_URL));
        shopname.setText(shop.get(CustomizedListView.KEY_NAME));
        thumb_image.setImageResource(imgID);
        //imageLoader.DisplayImage(shop.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
        
        return vi;
    }

public Filter getFilter() {
	// TODO Auto-generated method stub
	Filter filter=new Filter(){
		
		@SuppressWarnings("unchecked")
		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			// TODO Auto-generated method stub
			data=(ArrayList<HashMap<String,String>>) results.values; // Holds the results of a filtering operation in values
			notifyDataSetChanged(); //notifies the data with new filtered values
		}
		
	   
		@Override
		protected FilterResults performFiltering(CharSequence constraint) {
			// TODO Auto-generated method stub
			FilterResults results=new FilterResults(); // Holds the results of a filtering operation in values
			ArrayList<HashMap<String, String>> filteredArrList=new ArrayList<HashMap<String,String>>();
			HashMap<String, String> filteredShop=new HashMap<String, String>();
			
			if(dataOrigion == null){
				dataOrigion=new ArrayList<HashMap<String,String>>(data);
			}
			if(constraint == null || constraint.length() ==0){
				//set the Original result to return;
				results.count=dataOrigion.size();
				results.values=dataOrigion;
			}else{
				constraint =constraint.toString().toLowerCase();
				for(int i=0; i<dataOrigion.size(); i++){
					String dat=dataOrigion.get(i).get(CustomizedListView.KEY_NAME);
					if(dat.toLowerCase().startsWith(constraint.toString())){
						filteredShop.put(CustomizedListView.KEY_NAME, dataOrigion.get(i).get(CustomizedListView.KEY_NAME));
						filteredShop.put(CustomizedListView.KEY_URL, dataOrigion.get(i).get(CustomizedListView.KEY_URL));
						filteredShop.put(CustomizedListView.KEY_THUMB_URL, dataOrigion.get(i).get(CustomizedListView.KEY_THUMB_URL));
						filteredArrList.add(filteredShop);
					}
					
					results.count=filteredArrList.size();
					results.values=filteredArrList;
				}
			}
			return results;
		}
	};
	return filter;
}
}
